package com.gitee.feizns.explore.data_structure.array.easy;

/**
 * 643. 子数组最大平均数 I
 * @author feizns
 * @since 2020/3/23
 */
public class FindMaxAverage {

    public static void main(String[] args) {
        System.out.println(findMaxAverage(new int[]{1, 12, -5, -6, 50, 3}, 4));
    }

    public static double findMaxAverage(int[] nums, int k) {
        double sum = 0;
        int l = 0;
        int r = k - 1;
        for (int i = 0; i < k; i++)
            sum += nums[i];
        double ret = sum / k;
        while ( r < nums.length - 1 ) {
            sum = sum - nums[l] + nums[r + 1];
            l++;
            r++;
            ret = Math.max(ret, sum / k);
        }
        return ret;
    }

}
